#ifndef SHAPE_H
#define SHAPE_H
#include<qopengl.h>
#include <qopenglbuffer>
#include <qopenglvertexarrayobject>
#include <QOpenGLShaderProgram>
#include <QOpenGLDebugLogger>
#include <QVector>
#include <QStack>
#include "gltexture.h"

struct vtx_info{
    QVector3D _pos;
    QVector3D _color;
    QVector3D _normal;
    QVector2D _tex;
};

struct tri_info{
    int _v1;
    int _v2;
    int _v3;
};

class Shape : public QObject
{
Q_OBJECT
public:
    Shape();
    virtual ~Shape();
    
    bool init(QOpenGLShaderProgram* _p);

    void set_vertex_size(int n);
    void test_setup(int n);
    void set_vertex_data(QVector<vtx_info>&);

    void set_position(int _idx, QVector3D v);
    void set_color(int _idx, QVector3D c);
    void set_vnormal(int _idx, QVector3D v);
    void set_tex_info(int _index, QVector2D uv);

    void set_num_triangle(int n);
    void set_triangle(int _idx, tri_info _t);
    void set_triangle_data(QVector<tri_info>&);

    void enable_texture(bool);
    bool load_texture(QString file);

    void draw();

    void translate(QVector3D);
    void scale(float);
    void rotate(QVector3D, float);


    void setProjectionMatrix(QMatrix4x4 m){_proj_mat = m;}
    void setModelMatrix(QMatrix4x4 m){_model_mat = m;}

    bool load_obj(QString filename);

private:
    
    bool alloc_position(int _size);
	bool alloc_color(int _size);
    bool alloc_vnormal(int _size);
    bool alloc_texcoord(int _size);

	bool set_position_data(float *_data, int _size);
	bool set_color_data(float *_data, int _size);
    bool set_vnormal_data(float *_data, int _size);
    bool set_texcoord_data(float *_data, int _size);
    
	bool set_position(int _index, float _x, float _y, float _z);
	bool set_color(int _index, float _r, float _g, float _b);
    bool set_vnormal(int _idx, float _x, float _y, float _z);
    bool set_texcoord(int _index, float _u, float _v);
	
    bool set_position_subdata(float *_data, int _offset, int _size);
	bool set_color_subdata(float *_data, int _offset, int _size);
    bool set_vnormal_subdata(float *_data, int _offset, int _size);
    bool set_texcoord_subdata(float *_data, int _offset, int _size);
    
    bool prepare_buffer();

    QOpenGLVertexArrayObject *_vao;
	QOpenGLBuffer _position;
	QOpenGLBuffer _color;
    QOpenGLBuffer _normal;
	QOpenGLBuffer _texcoord;
    QOpenGLBuffer _index;
    
    QOpenGLShaderProgram *_shader_program;
    QOpenGLDebugLogger *_logger;
    
    QVector<GLTexture2D*> _tex_vector;

    QMatrix4x4 _proj_mat;
    QMatrix4x4 _model_mat;

    QMatrix4x4 _trans_mat;

    bool _texmode;
    
    
};

#endif // SHAPE_H
